#Replication material for: 
#When Censorship Works: Exploring the Resilience of News Websites to Online Censorship
#British Journal of Political Science
#Author: Philipp M. Lutscher

#Remove objects
rm(list = ls())

#Measure duration
start <- Sys.time()

###Load packages ####
library(tidyverse)

#This script runs the placebo tests that are reported in Online Appendix D
sample_traffic_final_out_egypt <- readRDS("processed/sample_traffic_final_out_egypt.rds")
sample_egypt_censored <- readRDS("processed/sample_egypt_censored.rds")

#### Placebo tests ####
run_placebo_control <- function(bw){
  set.seed(312)
  for (i in seq(10000)) {
    print(i)
    sample_traffic_main_placebo <- filter(sample_traffic_final_out_egypt, is.na(estimated_date_of_determining_blocking))
    
    estimated_date_of_determining_blocking <- sample_egypt_censored %>% group_by(site) %>% summarize(estimated_date_of_determining_blocking = unique(estimated_date_of_determining_blocking))
    site <-  sample(unique(sample_traffic_main_placebo$site),64)
    sample_traffic_main_placebo_site <- as.data.frame(site)
    sample_traffic_main_placebo_site$estimated_date_of_determining_blocking_placebo <- estimated_date_of_determining_blocking$estimated_date_of_determining_blocking
    
    sample_traffic_main_placebo_sites <- left_join(sample_traffic_main_placebo,sample_traffic_main_placebo_site,
                                                   by = "site",all.x = T)
    
    sample_traffic_main_placebo_sites <- filter(sample_traffic_main_placebo_sites,
                                                !is.na(estimated_date_of_determining_blocking_placebo)) %>% 
      mutate(before_after_time = date - estimated_date_of_determining_blocking_placebo,
             blocked = ifelse(before_after_time >= 0, 1, 0))
    
    #Filter Egyptian outlets and restrict to first and last date of censoring to avoid differences in the running variable
    sample_traffic_main_placebo_sites <- sample_traffic_main_placebo_sites %>% filter(before_after_time > -bw &
                                                                                        before_after_time < bw)
    
    lm_placebo <- lm(reach_per_million~blocked,filter(sample_traffic_main_placebo_sites,before_after_time != 0))
    
    if (i == 1) {
      placebo_control_out <- lm_placebo$coefficients[2]
    }
    else {
      placebo_control_out <- rbind(placebo_control_out, lm_placebo$coefficients[2])
    }
    
  }
  return(placebo_control_out)
}

#Takes a while
placebo_control_a <- run_placebo_control(bw = 30)
placebo_control_a <- as.data.frame(placebo_control_a)

#Plot density function
plot_placebo <-  ggplot(placebo_control_a,aes(x = blocked)) +
  geom_density(color = "black", fill = "gray") +
  theme_minimal() +
  theme(text = element_text(size = 30), panel.grid = element_blank(),
        panel.border = element_rect(colour = "black", fill = NA, size = 1),
        panel.spacing = unit(0, "in"),
        plot.margin = unit(c(5.5, 5.5, 5.5, 5.5), "points")) +
  labs(x = "Difference in average traffic per million", y = "Density") +
  geom_vline(xintercept = mean(placebo_control_a$blocked), linetype = "dotted", size = 2) +
  geom_segment(aes(x = mean(placebo_control_a$blocked) + 0.5, xend = mean(placebo_control_a$blocked) + 0.1, 
                   y = max(density(placebo_control_a$blocked)$y) + 
                     max(density(placebo_control_a$blocked)$y)/5, 
                   yend = max(density(placebo_control_a$blocked)$y) + 
                     max(density(placebo_control_a$blocked)$y)/5), arrow = arrow(length = unit(3, "mm"))
  ) +
  annotate(geom = "text", x = mean(placebo_control_a$blocked) + 0.7, y = max(density(placebo_control_a$blocked)$y) + 
             max(density(placebo_control_a$blocked)$y)/5, 
           label = paste("Sim. mean difference:",round(mean(placebo_control_a$blocked),2)), hjust = "left", size = 12) +
  xlim(min(placebo_control_a$blocked) - 2,max(placebo_control_a$blocked) + 2) +
  ylim(0,max(density(placebo_control_a$blocked)$y) + max(density(placebo_control_a$blocked)$y)/5)

ggsave("output/figures/figure_D1_placebo_control.pdf", plot_placebo,width = 16, height = 6)

#Measure duration
print(Sys.time() - start)
